package com.docker.drug.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.docker.drug.domain.DockerDrug;
import com.docker.drug.domain.DoctorDrugsOutIntermediate;
import com.docker.drug.mapper.DockerDrugMapper;
import com.docker.drug.mapper.DoctorDrugsOutIntermediateMapper;
import com.docker.drug.mapper.DoctorDrugsOutMapper;
import com.docker.drug.domain.DoctorDrugsOut;
import com.docker.drug.service.IDoctorDrugsOutService;

/**
 * 药品出库Service业务层处理
 * 
 * @author lin
 * @date 2025-04-12
 */
@Service
public class DoctorDrugsOutServiceImpl implements IDoctorDrugsOutService 
{
    @Autowired
    private DoctorDrugsOutMapper doctorDrugsOutMapper;
    @Autowired
    private DockerDrugMapper dockerDrugMapper;
    @Autowired
    private DoctorDrugsOutIntermediateMapper doctorDrugsOutIntermediateMapper;

    /**
     * 查询药品出库
     * 
     * @param id 药品出库主键
     * @return 药品出库
     */
    @Override
    public DoctorDrugsOut selectDoctorDrugsOutById(Long id)
    {
        return doctorDrugsOutMapper.selectDoctorDrugsOutById(id);
    }

    /**
     * 查询药品出库列表
     * 
     * @param doctorDrugsOut 药品出库
     * @return 药品出库
     */
    @Override
    public List<DoctorDrugsOut> selectDoctorDrugsOutList(DoctorDrugsOut doctorDrugsOut)
    {
        return doctorDrugsOutMapper.selectDoctorDrugsOutList(doctorDrugsOut);
    }

    /**
     * 新增药品出库
     * 
     * @param doctorDrugsOut 药品出库
     * @return 结果
     */
    @Override
    public int insertDoctorDrugsOut(DoctorDrugsOut doctorDrugsOut)
    {
        return doctorDrugsOutMapper.insertDoctorDrugsOut(doctorDrugsOut);
    }

    /**
     * 修改药品出库
     * 
     * @param doctorDrugsOut 药品出库
     * @return 结果
     */
    @Override
    public int updateDoctorDrugsOut(DoctorDrugsOut doctorDrugsOut)
    {
        if (doctorDrugsOut.getAuditStatus()==1){
            System.out.println(doctorDrugsOut);
            String drugsOutId = doctorDrugsOut.getDrugsOutId();

            List<DoctorDrugsOutIntermediate> list = doctorDrugsOutIntermediateMapper.selectDrugsOrderId(drugsOutId);

            for(DoctorDrugsOutIntermediate doctorDrugsOutIntermediate : list){
                String drugCode = doctorDrugsOutIntermediate.getDrugCode();
//                获取药品数量
                Long quantity = doctorDrugsOutIntermediate.getQuantity();
                //   根据药品编码查询药品表 将库存-quantity
                DockerDrug dockerDrug = dockerDrugMapper.selectDockerDrugByCodes(drugCode);
                dockerDrug.setDrugInventory(dockerDrug.getDrugInventory()-quantity);
//                修改库存
                dockerDrugMapper.updateDockerDrug(dockerDrug);

            }


        }


        return doctorDrugsOutMapper.updateDoctorDrugsOut(doctorDrugsOut);
    }

    /**
     * 批量删除药品出库
     * 
     * @param ids 需要删除的药品出库主键
     * @return 结果
     */
    @Override
    public int deleteDoctorDrugsOutByIds(Long[] ids)
    {
        return doctorDrugsOutMapper.deleteDoctorDrugsOutByIds(ids);
    }

    /**
     * 删除药品出库信息
     * 
     * @param id 药品出库主键
     * @return 结果
     */
    @Override
    public int deleteDoctorDrugsOutById(Long id)
    {
        return doctorDrugsOutMapper.deleteDoctorDrugsOutById(id);
    }
}
